package ydplugin.popup.actions;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.yingdong.tools.DBSchemaGenerator;
import com.yingdong.tools.ExcelTemplateGenerator;

public class GenerateDateBaseTemplateAction implements IObjectActionDelegate {

	private Shell shell;
	private ISelection currentSelection;

	/**
	 * Constructor for Action1.
	 */
	public GenerateDateBaseTemplateAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {

		if (currentSelection != null) {
			Object element = ((TreeSelection) currentSelection).getFirstElement();

			IProject project = ((IResource) element).getProject();
			if (element instanceof IFile) {
				IFile file = (IFile) element;
				try {
					DBSchemaGenerator.generateDBSchemaTemplate(file);
					MessageDialog.openInformation(shell, "Ydplugin", "生成成功!");
				} catch (Exception e1) {
					MessageDialog.openInformation(shell, "Ydplugin", e1.getMessage());
				}

			}

		}

	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		currentSelection = selection;
	}

}
